package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.dto.NoticeDTO;
import com.sky.dto.NoticePageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.NoticeService;
import com.sky.vo.NoticeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/notice")
@Api(tags = "公告管理接口")
@Slf4j
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    @PostMapping
    @ApiOperation("新增公告")
    public Result<String> save(@RequestBody NoticeDTO noticeDTO) {
        noticeService.save(noticeDTO);
        return Result.success(MessageConstant.NOTICE_ADD_SUCCESS);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询公告")
    public Result<PageResult> pageQuery(NoticePageQueryDTO noticePageQueryDTO) {
        PageResult pageResult = noticeService.pageQuery(noticePageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询公告")
    public Result<NoticeVO> getById(@PathVariable Long id) {
        NoticeVO noticeVO = noticeService.getById(id);
        return Result.success(noticeVO);
    }

    @PutMapping
    @ApiOperation("修改公告")
    public Result<String> update(@RequestBody NoticeDTO noticeDTO) {
        noticeService.update(noticeDTO);
        return Result.success(MessageConstant.NOTICE_UPDATE_SUCCESS);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("启用/禁用公告")
    public Result<String> updateStatus(Long id, @PathVariable Integer status) {
        noticeService.updateStatus(id, status);
        return Result.success(MessageConstant.NOTICE_STATUS_UPDATE_SUCCESS);
    }

    @GetMapping("/latest")
    @ApiOperation("获取最新公告")
    public Result<NoticeVO> getLatestNotice() {
        NoticeVO noticeVO = noticeService.getLatestNotice();
        return Result.success(noticeVO);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        noticeService.deleteNotice(id);
        return Result.success();
    }
}